package cn.zhuo.knowledge.util.guava.util.concurrent.ratelimiter;

import cn.hutool.core.thread.ThreadUtil;
import com.google.common.util.concurrent.RateLimiter;

import java.util.concurrent.TimeUnit;

/**
 * <p>平滑突发限流，令牌桶算法
 *
 * @author z2huo
 */
class RateLimiterDemo {

    public static void main(String[] args) {
        // 每秒产生 5 个令牌
        RateLimiter rateLimiter = RateLimiter.create(5);
        ThreadUtil.sleep(1, TimeUnit.SECONDS);
        for (int i = 0; i < 20; i++) {
            double sleepingTime = rateLimiter.acquire(1);
            System.out.printf("get 1 tokens, %ss%n", sleepingTime);
        }
    }

}
